<?php
/**
 * server credentials model
 *
 * @author raphael seebacher <raphasee@ee.ethz.ch>
 * @version 0.01
 */
class server_credentials extends db_model {
    protected $data = Array(
      'id' => NULL,
      'consumer_key' => NULL,
      'consumer_secret' => NULL,
    );

    public function save() {
        return false;
    }

    public function get_key() {
        return $this->get('consumer_key');
    }

    public function get_secret() {
        return $this->get('consumer_secret');
    }

    public static function get_newest() {
        $dbc = db_controller::get_instance();
        $q = 'SELECT * '
            .'FROM server_credentials '
            .'ORDER BY id DESC '
            .'LIMIT 1';
        $server_credentials_raw = $dbc->query_assoc($q);

        if (!is_array($server_credentials_raw)
            || !count($server_credentials_raw)
        ) {
            throw new model_exception('Error getting server credentials.');
        }

        return new self($server_credentials_raw[0]);
    }

    public static function create_from_id($id) {
        $q = 'SELECT * '
            .'FROM server_credentials '
            .'WHERE id='.self::$dbc->escape($id);
        $server_credentials_raw = self::$dbc->query_assoc($q);

        if (!is_array($server_credentials_raw)
            || !count($server_credentials_raw)
        ) {
            throw new model_exception('Error getting server credentials.');
        }

        return new self($server_credentials_raw[0]);
    }
}
?>
